﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.employee
{
    public partial class login : System.Web.UI.Page
    {
        // private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            // db = new RailwayReservationDataContext();
            pMessage.Visible = false;
        }

        private void ClearMessage()
        {
            pMessage.Visible = false;
            pMessage.InnerText = "";
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            ClearMessage();
            if (!Page.IsValid) return;
            var authinfo = new Account();
            var authsvc = new AuthService();
            authinfo.UserName = txtUsername.Text.Trim();
            authinfo.Password = Checksum.MD5(txtPassword.Text.Trim());
            // authinfo.Role = 2;
            var check = authsvc.CheckLogin(authinfo);
            if (check == true)
            {
                Session["authinfo"] = authinfo;
                switch (authinfo.Role)
                {
                    case 0:
                        Response.Redirect("~/default.aspx");
                        break;
                    case 1:
                        Response.Redirect("~/employee/default.aspx");
                        break;
                    case 2:
                        Response.Redirect("~/manager/default.aspx");
                        break;
                }
            }
            else ShowMessage("Invalid Username and Password combination");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            ClearMessage();
            txtUsername.Text = "";
            txtPassword.Text = "";
        }
    }
}
